﻿
package extremefx.ui {
	import extremefx.events.EventHandler;
	import extremefx.events.KeyEventArgs;
	import extremefx.events.MouseEventArgs;
	import extremefx.geom.Size;
	import extremefx.ui.UIComponent;
	import extremefx.ui.styles.UIStates;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.ui.Keyboard;	

	/**
	 * @author Marcelo Volmaro
	 */
	public class Checkbox extends UIComponent {
		private var _label:Label;
		private var _checked:Boolean;
		private var _mh:ButtonSprite;
		
		public function Checkbox(pLabel:String = "", pParameters:UIParameters = null) {
			super();
			
			pParameters = initStyle("Checkbox", pParameters);
			pParameters._parent = this;
			_label = new Label(pLabel, pParameters);

			var vui:Sprite = _visualUI; 
			_mSprite.addChild(_label._visualUI);
			
			var bd:BitmapData = _style.getFirstBMPData();
			_bmpW = bd.width;
			_bmpH = bd.height;
			
			_label.setBounds(bd.width, _style.padding.top);
			
			var s:Size = _label.size = _label.preferredSize;
			_width = bd.width + _style.padding.left + _style.padding.right + s.width;
			_height = Math.max(bd.height, s.height + _style.padding.top + _style.padding.bottom);
			_fheight = true;

			_mh = new ButtonSprite(vui);
			_mh.onRollOver.add(_rollOver);
			
			_mh.onRollOut.add(_rollOut);
			_mh.onReleaseOutside.add(_rollOut);
			_mh.onDragOut.add(_rollOut);
			
			_mh.onPress.add(_press);
			_mh.onDragOver.add(_press);
			
			_mh.onRelease.add(_release);
			
			invalidate();
		}
		
		private var _bmpW:int;
		private var _bmpH:int;

		public function get checked():Boolean {
			return _checked;
		}
		
		public function set checked(pChecked:Boolean):void {
			_checked = pChecked;
			_setView();
		}

		private function _rollOut(pSender:ButtonSprite, pArgs:MouseEventArgs):void{
			_setView();
		}
		
		private function _rollOver(pSender:ButtonSprite, pArgs:MouseEventArgs):void{
			_setView(UIStates.HOVER);
		}
		
		private function _press(pSender:ButtonSprite, pArgs:MouseEventArgs):void{
			_setView(UIStates.ACTIVE);
		}
		
		private function _release(pSender:ButtonSprite, pArgs:MouseEventArgs):void{
			_checked = !_checked;
			focus();
			_setView();
			if (_eOnChange) _eOnChange.fire();
		}
		
		override protected function _keyDownHandler(pSender:KeyHandler, pArgs:KeyEventArgs):void {
			if (pArgs.keyboardEvent.keyCode == Keyboard.SPACE && !_pressOnce){
				_press(null, null);
				_pressOnce = true;
			}
		}
		
		override protected function _keyUpHandler(pSender:KeyHandler, pArgs:KeyEventArgs):void {
			if (pArgs.keyboardEvent.keyCode == Keyboard.SPACE && _pressOnce){
				_release(null, null);
				_pressOnce = false;
			}
		}
		
		private function _setView(pState:uint = UIStates.NORMAL):void{
			pState = _getState(pState, _checked);

			_label.setState(pState);
			_setDecoState(pState, _width, _height);
		}
		
		override public function dispose():void {
			if (_disposing) return;
			super.dispose();
			_mh.dispose();
			
			_eOnChange.dispose();
			_label.dispose();
		}
		
		override public function get preferredSize():Size {
			var lh:Size = _label.preferredSize;
			lh.width += _bmpW;
			lh.height += _bmpH;
			return lh;
		}
		
		override public function get minimumSize():Size {
			return preferredSize;
		}
		
		override protected function _draw():void {
			_mh.enabled = _enabled;
			_setView();
		}
		
		private var _eOnChange:EventHandler;
		public function get onChange():EventHandler{
			if (_eOnChange == null) _eOnChange = new EventHandler(this);
			return _eOnChange;
		}
	}
}
